package com.hansen.library.pickerimage;

import android.app.Activity;
import android.content.Context;

import com.hansen.library.R;
import com.hansen.library.ui.widget.dialog.ActionSheetDialog;
import com.hansen.library.ui.fragment.BaseFragment;
import com.hansen.library.utils.StringUtils;
import com.hansen.library.utils.storage.StorageType;
import com.hansen.library.utils.storage.StorageUtils;
import com.hansen.library.pickerimage.activity.PickImageActivity;

/**
 * Created by huangjun on 2015/9/22.
 */
public class PickImageHelper {

    public static class PickImageOption {
        /**
         * 图片选择器标题
         */
        public int titleResId = R.string.choose;

        /**
         * 是否多选
         */
        public boolean multiSelect = true;

        /**
         * 最多选多少张图（多选时有效）
         */
        public int multiSelectMaxCount = 9;

        /**
         * 是否进行图片裁剪(图片选择模式：false / 图片裁剪模式：true)
         */
        public boolean crop = false;

        /**
         * 图片裁剪的宽度（裁剪模式时有效）
         */
        public int cropOutputImageWidth = 720;

        /**
         * 图片裁剪的高度（裁剪模式时有效）
         */
        public int cropOutputImageHeight = 720;

        /**
         * 图片选择保存路径
         */
        public String outputPath = StorageUtils.getWritePath(StringUtils.get32UUID() + ".jpg", StorageType.TYPE_TEMP);
    }

    /**
     * 打开图片选择器
     */
    public static void pickImage(final Context context, final int requestCode, final PickImageOption option) {
        if (context == null) {
            return;
        }

        new ActionSheetDialog(context)
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem(context.getString(R.string.camera), ActionSheetDialog.SheetItemColor.Black,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                int from = PickImageActivity.FROM_CAMERA;
                                if (!option.crop) {
                                    PickImageActivity.start((Activity) context, requestCode, from, option.outputPath, option.multiSelect, 1,
                                            true, false, 0, 0);
                                } else {
                                    PickImageActivity.start((Activity) context, requestCode, from, option.outputPath, false, 1,
                                            false, true, option.cropOutputImageWidth, option.cropOutputImageHeight);
                                }
                            }
                        })
                .addSheetItem(context.getString(R.string.text_choose_from_photo_album), ActionSheetDialog.SheetItemColor.Black,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                int from = PickImageActivity.FROM_LOCAL;
                                if (!option.crop) {
                                    PickImageActivity.start((Activity) context, requestCode, from, option.outputPath, option.multiSelect,
                                            option.multiSelectMaxCount, true, false, 0, 0);
                                } else {
                                    PickImageActivity.start((Activity) context, requestCode, from, option.outputPath, false, 1,
                                            false, true, option.cropOutputImageWidth, option.cropOutputImageHeight);
                                }
                            }
                        }).show();
    }

    /**
     * 打开图片选择器
     */
    public static void pickImage(final BaseFragment fragment, final int requestCode, final PickImageOption option) {
        if (fragment == null) {
            return;
        }

        new ActionSheetDialog(fragment.getContext())
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem(fragment.getString(R.string.camera), ActionSheetDialog.SheetItemColor.Black,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                int from = PickImageActivity.FROM_CAMERA;
                                if (!option.crop) {
                                    PickImageActivity.start(fragment, requestCode, from, option.outputPath, option.multiSelect, 1,
                                            true, false, 0, 0);
                                } else {
                                    PickImageActivity.start(fragment, requestCode, from, option.outputPath, false, 1,
                                            false, true, option.cropOutputImageWidth, option.cropOutputImageHeight);
                                }
                            }
                        })
                .addSheetItem(fragment.getString(R.string.text_choose_from_photo_album), ActionSheetDialog.SheetItemColor.Black,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                int from = PickImageActivity.FROM_LOCAL;
                                if (!option.crop) {
                                    PickImageActivity.start(fragment, requestCode, from, option.outputPath, option.multiSelect,
                                            option.multiSelectMaxCount, true, false, 0, 0);
                                } else {
                                    PickImageActivity.start(fragment, requestCode, from, option.outputPath, false, 1,
                                            false, true, option.cropOutputImageWidth, option.cropOutputImageHeight);
                                }
                            }
                        }).show();
    }

    /**
     * 打开图片选择器
     */
    public static void pickImageFromAlbum(final Context context, final int requestCode, final PickImageOption option) {
        if (context == null) {
            return;
        }

        int from = PickImageActivity.FROM_LOCAL;
        if (!option.crop) {
            PickImageActivity.start((Activity) context, requestCode, from, option.outputPath, option.multiSelect,
                    option.multiSelectMaxCount, true, false, 0, 0);
        } else {
            PickImageActivity.start((Activity) context, requestCode, from, option.outputPath, false, 1,
                    false, true, option.cropOutputImageWidth, option.cropOutputImageHeight);
        }
    }
}
